
package com.yushu.common.execption;

import org.apache.log4j.Logger;


 /**
 * 基础异常类
 * @author test
 * @CreateDate 2020-12-03 15:47:24
 */
public class CgException extends RuntimeException {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private String message;

    private boolean msgFlag = true;


    public CgException(String info) {
        super(info);
        this.message = info;
        this.msgFlag = false;
    }

    /**
     * 业务异常
     *
     * @param info 异常信息
     * @param flag 是否错误异常， true 服务器异常，事务回滚，不提示,false 业务异常，事务回滚，提示给用户
     */
    public CgException(String info, boolean flag, Class clazz) {
        super(info);
        if (null != clazz) {
            if (null == info) {
                info = "消息内容为空";
            }
            this.message = info;
            this.msgFlag = flag;
            Logger log = Logger.getLogger(clazz);
            log.info(info);
        }
    }

    /**
     * 人为创建一个CgException异常 使用info级
     */
    public CgException(String info, Class clazz) {
        super(info);
        if (null != clazz) {
            if (null == info) {
                info = "消息内容为空";
            }
            this.message = info;
            this.msgFlag = false;
            Logger log = Logger.getLogger(clazz);
            log.info(info);
        }
    }

    /**
     * 人为创建一个CgException异常 使用error级,向日志中记录消息同时记录堆栈信息
     */
    public CgException(String error, Exception e, Class clazz) {
        super(e);
        message = error;
        this.msgFlag = true;
        Logger log = Logger.getLogger(clazz);
        log.error(message);
        if (null != e) {
            log.error(e.toString());
            StackTraceElement[] trace = this.getCause().getStackTrace();
            for (StackTraceElement element : trace) {
                log.error("\tat " + element);
            }
            // 当日志中记录的错误信息不足以分析出错误原因.需要在这里追加引起exception"causedException"的错误堆栈输出.
        }
        writeErrorLog(message, e, log);
    }
    

    protected void writeErrorLog(String error, Throwable e, Logger log) {
        StringBuffer sb = new StringBuffer(error + " Cause At ");
        if (e.getStackTrace() != null) {
            int n = e.getStackTrace().length;
            for (int i = 0; i < n; i++) {
                String tmp = e.getStackTrace()[i].toString();
                if (tmp.toLowerCase().indexOf("weblogic") != -1) {
                    break;
                }
                sb.append("\r\n\t").append(tmp);
            }
        }
        log.error(sb.append("\r\n").toString());
    }

    /**
     * 方法使用范围： 需要记录详细堆栈信息的情况 如CheckedException 注意： 不要主动抛出Java自带的异常
     * 主动抛出异常必须是自定义异常
     */
    public CgException(String message, Throwable cause) {
        super(message, cause);
    }

    public CgException(Throwable e, Class clazz) {
        super(e);
        this.msgFlag = true;
        Logger log = Logger.getLogger(clazz);
        log.error(message);
        if (null != e) {
            message = e.getMessage();
            log.error(e.toString());
            StackTraceElement[] trace = this.getCause().getStackTrace();
            for (StackTraceElement element : trace) {
                log.error("\tat " + element);
            }
            // 当日志中记录的错误信息不足以分析出错误原因.需要在这里追加引起exception"causedException"的错误堆栈输出.
        }
        writeErrorLog(this.getMessage(), this, log);
    }
}
